﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
using Ruminate.Utils;
using System.Xml;

namespace SimpleGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    
    public enum GameMode{init=0,intro,map,room};

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;

        public SpriteFont font12;
        public Texture2D GreyImageMap;
        public string GreyMap;

        public Texture2D GreyImageMap2;
        public string GreyMap2;

        public MainUI mainUI;
        public IntroMenu introMenu;
        public GameMap gameMap;
        public ChooseWork chooseWork;

        Vector2 mousePos;
        public Vector2 screenSize;

        
        public GameMode gameMode;

        public bool TimeRun = true;


        /*맵 정보*/
        bool[] UsingDept;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            screenSize.X = 1024;
            screenSize.Y = 576;

            graphics.PreferredBackBufferHeight = (int)screenSize.Y;
            graphics.PreferredBackBufferWidth = (int)screenSize.X;

            this.IsMouseVisible = true;
            
            
        }

        public MainUI getMainUI(){
            return mainUI;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            gameMode = GameMode.map;
            

            mousePos.X = -9999;
            mousePos.Y = -9999;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            font12 = Content.Load<SpriteFont>("Font12");
            
            GreyImageMap = Content.Load<Texture2D>(@"GreySkin\ImageMap");
            GreyMap = File.OpenText(@"Content\GreySkin\Map.txt").ReadToEnd();

            GreyImageMap2 = Content.Load<Texture2D>(@"GreySkin\ImageMap2");
            GreyMap2 = File.OpenText(@"Content\GreySkin\Map2.txt").ReadToEnd();

            DebugUtils.Init(graphics.GraphicsDevice, font12);

            mainUI = new MainUI();
            introMenu = new IntroMenu();
            gameMap = new GameMap();
            chooseWork = new ChooseWork();

            mainUI.Init(this);
            introMenu.Init(this);
            gameMap.Init(this);
            chooseWork.Init(this);



            LoadMap("testMap.xml");
        }

        public void LoadMap(string fileName)
        {
            //LoadingMap 파트 '완벽히' 새로 짤것
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);


            var xRoot = xmlDoc.SelectSingleNode("/snp_save");

            var mapValue = xRoot.SelectSingleNode("map/name");
            string mapName = mapValue.InnerText;
            Texture2D mapTexture;
            using (FileStream fileStream = new FileStream(@"maps\"+mapName+".png", FileMode.Open))
            {
                mapTexture = Texture2D.FromStream(graphics.GraphicsDevice,fileStream);
            }

            List<Building> tmpBuildings = new List<Building>();
            tmpBuildings.Add(new BuildingLab(this, new Vector2(200, 200)));
            tmpBuildings.Add(new BuildingLecture(this, new Vector2(455, 371)));
            tmpBuildings.Add(new BuildingEmpty(this, new Vector2(200, 550)));

            Type enumType = Type.GetType("SimpleGame.Dept");
            int deptLength = Enum.GetValues(enumType).Length;
            UsingDept = new bool[deptLength];

            for (int i = 0; i < deptLength; i++)
            {
                UsingDept[i] = true;
            }

            gameMap.LoadMap(mapTexture, tmpBuildings);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            if (TimeRun)
            {
                //실시간 변경 데이터 추가
            }

            if (gameMode == GameMode.init)
            {
            }
            else if (gameMode == GameMode.intro)
            {
                introMenu.Update(gameTime);
            }
            else if (gameMode == GameMode.map)
            {
                mainUI.Update(gameTime);
                gameMap.Update(gameTime);
                
            }
            else
            {
                mainUI.Update(gameTime);
                if (gameMode == GameMode.room)
                {
                    chooseWork.Update(gameTime);
                }
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            

            if (gameMode == GameMode.init)
            {
                // TODO : 초기화 화면(로고라던가) 출력
            }
            else if (gameMode == GameMode.intro)
            {
                introMenu.Draw(gameTime);
            }
            else if (gameMode == GameMode.map)
            {
                gameMap.Draw(gameTime);
                mainUI.Draw(gameTime);
            }
            else if( gameMode == GameMode.room)
            {
                chooseWork.Draw(gameTime);
                mainUI.Draw(gameTime);
            }
            // TODO: Add your drawing code here
            

            base.Draw(gameTime);
        }
    }
}
